package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Address;
import com.atguigu.service.AddRessService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddRessController {

    @Reference
    AddRessService addRessService;

    @RequestMapping("/findAllMaps")
    public Map findAllMaps(){
        Map map = new HashMap();

        List<Address> list = addRessService.findAll();

        List<Map> gridnMaps = new ArrayList<>();
        List<Map> nameMaps = new ArrayList<>();

        for (Address address : list) {
            Map gridnMap = new HashMap();
            gridnMap.put("lng",address.getLng());
            gridnMap.put("lat",address.getLat());
            gridnMaps.add(gridnMap);

            Map nameMap = new HashMap();
            nameMap.put("addressName",address.getAddressName());
            nameMaps.add(nameMap);
        }

        map.put("gridnMaps",gridnMaps);
        map.put("nameMaps",nameMaps);

        return map;
    }


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
            pageResult = addRessService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            addRessService.deleteById(id);
            return new Result(true,"删除分公司地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除分公司地址失败");
        }
    }

    /**
     * 添加操作
     */
    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Address address){
        try {
            addRessService.addAddress(address);
            return new Result(true,"添加分公司地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加分公司地址失败");
        }
    }


}
